package com.mlm.mlmapp.adapter.view.adapter;

import org.json.JSONArray;
import org.json.JSONException;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.Toast;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.mlm.mlmapp.adapter.view.listener.OnSyClickedListener;
import com.mlm.mlmapp.cons.HTTPCons;

public class MyPagerAdapter extends PagerAdapter implements  HTTPCons{
	AQuery aq;	
	Context con;
		ImageView image;
		private JSONArray array;
		private String actID;
		private JSONArray contents;
		private OnSyClickedListener onSyListener;
		public MyPagerAdapter(Context con, JSONArray array ,OnSyClickedListener onSyListener) {
			// this.viewList=viewList;
			this.array = array;
			this.con = con;
			aq=new AQuery(con);
			this.onSyListener=onSyListener;
		}

		@SuppressLint("NewApi")
		@Override
		public Object instantiateItem(final ViewGroup container,
				final int position) {
			
			image = new ImageView(con);
			image.setAdjustViewBounds(true);
			image.setLayoutParams(new LayoutParams(200,-1));
//			image=(ImageView)inf.inflate(R.layout.include_view_pager, null).findViewById(R.id.iv_sylist);
//			image.setAdjustViewBounds(true);
////			image.setScaleType(ScaleType.CENTER_CROP);
			try {
				image.setTag(array.getJSONObject(position).get(ID_SYLIST)
						.toString());
				
				aq.ajax(array.getJSONObject(position).get(IMG_SYLIST)
						.toString(), Bitmap.class, new AjaxCallback<Bitmap>() {

					@Override
					public void callback(String url, Bitmap object,
							AjaxStatus status) {
						if(status.getCode()==-101){
							Toast.makeText(aq.getContext(),"网络异常,请检查网络", Toast.LENGTH_SHORT);
							return;
						}
						try {
							final String id=array
							.getJSONObject(position).get(ID_SYLIST)
							.toString();
							image = (ImageView) container.findViewWithTag(id);
							image.setImageBitmap(object);
							image.setOnClickListener(new OnClickListener() {
								@Override
								public void onClick(View arg0) {
									// TODO Auto-generated method stub
									onSyListener.onSyItemClick(arg0, id);
								}
							});
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

						container.removeView(image);
						container.addView(image);
						// view.setBackgroundColor(Color.argb(255, 1 *
						// 50,
						// 1 * 10, 1 * 50));
					}
				});
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			container.removeView(image);
			container.addView(image);
			this.actID = image.getTag().toString();
			return image;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public int getCount() {
			return array.length();
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return (view == object);
		}
//
		@Override
		public float getPageWidth(int position) {
			// TODO Auto-generated method stub
			return 1f;
		}

		public String getActID(int position) {
			String str="";
			try {
				str= array.getJSONObject(position).get(ID_SYLIST)
						.toString();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return str;
		}

	}